﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL.Forms;
using UCL.Data;
using UCL;

namespace DBKit5.UControls
{
    /// <summary>
    /// 数据库连接面板控件
    /// </summary>
    /// <remarks>2014-1-21 by sutroon</remarks>
    public partial class ucConnectionBar : UserControl
    {
        /// <summary>
        /// 获取或设置重新选择连接对象下拉框的委托方法
        /// </summary>
        public Action<IDatabaseHelper> ConnectionChangedHandler { get; set; }

        /// <summary>
        /// 获取或设置执行按钮的委托方法
        /// </summary>
        public Action<string> RunSqlHandler { get; set; }
        AppDataHelper adh;
        public ucConnectionBar()
        {
            InitializeComponent();
            this.Size = new Size(313, 21);

            ButtonClickEvent();
            cbConnections.SelectedIndexChanged+=new EventHandler(cbConnections_SelectedIndexChanged);
        }
        public void uc_init(bool showRunBtn=false)
        {
            btnRun.Visible = showRunBtn;

            adh = new AppDataHelper();
            LoadConnections();
        }

        /// <summary>
        /// 加载连接数据集到下拉控件中
        /// </summary>
        /// <remarks>2014-1-21 by sutroon</remarks>
        private void LoadConnections()
        {
            // 读取连接
            DataTable dtCons = adh.ExecuteDataTable("select * from tconnections");
            cbConnections.SetDataSource(dtCons);
            DataRow[] rowCons = dtCons.Select("IsDefault=1");
            if (rowCons.Length > 0) cbConnections.SelectedValue = rowCons[0]["ID"];
        }

        /// <summary>
        /// 配置按钮点击事件
        /// </summary>
        protected void ButtonClickEvent()
        {
            btnAdd.Click += new EventHandler(Button_Click);
            btnDetail.Click += new EventHandler(Button_Click);
            btnRefresh.Click += new EventHandler(Button_Click);
            btnRun.Click += new EventHandler(Button_Click);
        }

        /// <summary>
        /// 按钮点击事件处理函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, EventArgs e)
        {
            switch (((Button)sender).Name)
            {
                case "btnAdd":
                    new frmConnectionEdit() { RefreshHandler = LoadConnections }.Show();
                    break;
                case "btnDetail":
                    if (cbConnections.SelectedItem == null) return;
                    DataRowView rConn = ((DataRowView)cbConnections.SelectedItem);
                    new frmConnectionEdit() { RecordID = int.Parse(rConn["ID"].ToString()), ReadOnly = true }.Show();
                    break;
                case "btnRefresh":
                    this.LoadConnections();
                    break;
                case "btnRun":
                    if (RunSqlHandler != null) RunSqlHandler(string.Empty);
                    break;
            }
        }

        /// <summary>
        /// 下拉控件选项切换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbConnections_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbConnections.SelectedItem == null || ConnectionChangedHandler == null) return;
            DataRowView rConn = ((DataRowView)cbConnections.SelectedItem);

            // 测试连接
            if (!adh.TestConnection(rConn["ProductName"].ToString(), rConn["HostName"].ToString(), rConn["Port"].ToString().ToInt())) return;
            if (ConnectionChangedHandler != null) ConnectionChangedHandler(adh.GetDatabaseHelper(rConn["ProductName"].ToString(), rConn["HostName"].ToString(), rConn["LoginID"].ToString(), rConn["Password"].ToString(), rConn["Database"].ToString()));
        }
    }
}
